<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        var users = [{
                id: 1,
                name: '小明',
                sex: 1
            }, {
                id: 1,
                name: '小1明',
                sex: 2
            }, {
                id: 1,
                name: '小明',
                sex: 1
            }, {
                id: 1,
                name: '小2明',
                sex: 2
            },

        ]
        var sex = [{
                id: 1,
                sex: '男'
            }, {
                id: 2,
                sex: '女'
            }]
            // 单一归类
        var cache = {};
        sex.forEach(function(item) {
            var _id = item.id
            cache[_id] = [];
            users.forEach(function(item) {
                var _sex = item.sex;
                if (_id === _sex) {
                    cache[_id].push(item)
                }
            })
        })
        console.log(cache);
        var hobby = [{
            id: 1,
            name: '足球'
        }, {
            id: 2,
            name: '足球1'
        }, {
            id: 3,
            name: '足球2'
        }, {
            id: 4,
            name: '足球3'
        }, {
            id: 5,
            name: '足球4'
        }, ]
        var person = [{
                name: '小明',
                hobby: '1,2'
            }, {
                name: '小红',
                hobby: '2,4'
            }, {
                name: '小黄',
                hobby: '1,3'
            }]
            // 复合归类
        var cache = {}
        hobby.forEach(function(hobby) {
            var _id = hobby.id;
            cache[_id] = [];
            person.forEach(function(person) {
                var arr = person.hobby.split(',');
                console.log(arr);
                arr.forEach(function(item) {
                    if (_id == item) {
                        cache[_id].push(person)
                    }
                })
            })
        })
        console.log(cache);
        console.log('----------');
        var singleSort = sortDatas(sex, users);
        console.log(singleSort('sex', 'single'));
        var singleSort1 = sortDatas(hobby, person);
        console.log(singleSort1('hobby', 'multi'));

        function sortDatas(sort, data) {
            var cache = {};

            return function(foreign_key, sortType) {
                if (sortType !== 'single' && sortType !== 'multi') {
                    console.log(new Error('不是合法归类类型'));
                    return
                }
                sort.forEach(function(sort) {
                    var _id = sort.id;
                    cache[_id] = [];
                    data.forEach(function(elem) {
                        var foreign_val = elem[foreign_key];
                        switch (sortType) {
                            case 'single':
                                if (foreign_val == _id) {
                                    cache[_id].push(elem);
                                }
                                break;
                            case 'multi':
                                var _arr = foreign_val.split(',');
                                _arr.forEach(function(val) {
                                    if (val == _id) {
                                        cache[_id].push(elem);
                                    }
                                })
                                break;
                            default:
                                break;
                        }
                    })
                })
                return cache
            }
        }
    </script>
</body>

</html>